package com.zys.http.processor.jaxrs;

import com.intellij.psi.PsiAnnotation;
import com.zys.http.constant.HttpEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhou ys
 * @since 2024-05-21
 */
public interface JaxRsEnums {
    @Getter
    @AllArgsConstructor
    enum Controller {
        PATH_JAVAX("javax.ws.rs.Path"),

        PATH_JAKARTA("jakarta.ws.rs.Path");

        private final String clazz;

        public String getShortClassName() {
            return clazz.substring(clazz.lastIndexOf('.') + 1);
        }
    }

    @Getter
    @AllArgsConstructor
    enum Method {
        GET_JAVAX("javax.ws.rs.GET", HttpEnum.HttpMethod.GET),
        POST_JAVAX("javax.ws.rs.POST", HttpEnum.HttpMethod.POST),
        PUT_JAVAX("javax.ws.rs.PUT", HttpEnum.HttpMethod.PUT),
        DELETE_JAVAX("javax.ws.rs.DELETE", HttpEnum.HttpMethod.DELETE),
        PATCH_JAVAX("javax.ws.rs.PATCH", HttpEnum.HttpMethod.PATCH),

        GET_JAKARTA("jakarta.ws.rs.GET", HttpEnum.HttpMethod.GET),
        POST_JAKARTA("jakarta.ws.rs.POST", HttpEnum.HttpMethod.POST),
        PUT_JAKARTA("jakarta.ws.rs.PUT", HttpEnum.HttpMethod.PUT),
        DELETE_JAKARTA("jakarta.ws.rs.DELETE", HttpEnum.HttpMethod.DELETE),
        PATCH_JAKARTA("jakarta.ws.rs.PATCH", HttpEnum.HttpMethod.PATCH);

        private static final Map<String, HttpEnum.HttpMethod> METHOD_MAP;

        static {
            METHOD_MAP = Arrays.stream(Method.values())
                    .collect(Collectors.toMap(Method::getClazz, Method::getHttpMethod));
        }

        private final String clazz;
        private final HttpEnum.HttpMethod httpMethod;

        public static boolean contains(@NotNull PsiAnnotation annotation) {
            String qualifiedName = annotation.getQualifiedName();
            return METHOD_MAP.containsKey(qualifiedName);
        }

        public static boolean contains(@NotNull String name) {
            return METHOD_MAP.containsKey(name);
        }

        public static @Nullable HttpEnum.HttpMethod get(@NotNull PsiAnnotation annotation) {
            String qualifiedName = annotation.getQualifiedName();
            if (!METHOD_MAP.containsKey(qualifiedName)) {
                return null;
            }
            HttpEnum.HttpMethod httpMethod = METHOD_MAP.get(qualifiedName);
            if (HttpEnum.HttpMethod.REQUEST.equals(httpMethod)) {
                httpMethod = HttpEnum.HttpMethod.requestMappingConvert(annotation);
            }
            return httpMethod;
        }

        public static @Nullable HttpEnum.HttpMethod get(@NotNull String qualifiedName) {
            return METHOD_MAP.get(qualifiedName);
        }
    }

    @Getter
    @AllArgsConstructor
    enum Param {
        PATH_PARAM_JAVAX("javax.ws.rs.PathParam", HttpEnum.ParamUsage.PATH),
        QUERY_PARAM_JAVAX("javax.ws.rs.QueryParam", HttpEnum.ParamUsage.URL),
        HEADER_PARAM_JAVAX("javax.ws.rs.HeaderParam", HttpEnum.ParamUsage.HEADER),

        PATH_PARAM_JAKARTA("jakarta.ws.rs.PathParam", HttpEnum.ParamUsage.PATH),
        QUERY_PARAM_JAKARTA("jakarta.ws.rs.QueryParam", HttpEnum.ParamUsage.URL),
        HEADER_PARAM_JAKARTA("jakarta.ws.rs.HeaderParam", HttpEnum.ParamUsage.HEADER);

        private final String clazz;
        private final HttpEnum.ParamUsage paramUsage;
    }
}
